package com.tgu.home_action.service.impl;

import com.alibaba.fastjson.JSON;
import com.tgu.home_action.dao.TrackPointMapper;
import com.tgu.home_action.pojo.ResponseData;
import com.tgu.home_action.pojo.Track;
import com.tgu.home_action.pojo.TrackPoint;
import com.tgu.home_action.pojo.state.RedisHeader;
import com.tgu.home_action.pojo.state.ResponseState;
import com.tgu.home_action.service.TrackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TrackServiceImpl implements TrackService {
    @Autowired
    private TrackPointMapper trackPointMapper;
    @Autowired
    private RedisTemplate<String, TrackPoint> trackPointRedisTemplate;
    /**
     * 查询轨迹
     */
    @Override
    public ResponseData<List<Track>> getTrack(Track track) {
        List<Track> trackList=trackPointMapper.getTrack(track);
        System.out.println("========================================");
        System.out.println("trackList: " + trackList);


        for(Track trackNode:trackList){
            System.out.println("trackNode:" + trackNode);
            List<TrackPoint> trackPoints=new ArrayList<>();


//            for(TrackPoint trackPoint:trackNode.getTrackPoints()){
//                System.out.println("trackPoints: " + trackPoint);
//                List<TrackPoint> trackPointNode= JSON.parseArray(trackPoint.getPoint(),TrackPoint.class);
//                if(trackPointNode != null){
//                    trackPoints.addAll(trackPointNode);
//                }
//            }

            List<TrackPoint> trackPointNode=trackPointRedisTemplate.opsForList().range(RedisHeader.TRACK.getHeader()+trackNode.getCaseId()+trackNode.getVolunteerId(), 0,-1);

//            if(trackPointNode!=null){
//                trackPoints.addAll(trackPointNode);
//            }
//            trackNode.setTrackPoints(trackPoints);
        }
        return new ResponseData<>(ResponseState.SUCCESS.getMessage(), ResponseState.SUCCESS.getValue(),trackList);
    }
}
